﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using CH341PAR;
using write.Properties;
using System.Drawing.Drawing2D;
using Sunny.UI;

namespace write
{ 
	public partial class Form1 : Form
	{
		public Form1()
		{
			this.InitializeComponent();
		}
		//退出按键
		private void Form1_FormClosing(object sender, FormClosingEventArgs e)
		{
			DialogResult dr = MessageBox.Show("是否退出?", "提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

			if (dr == DialogResult.OK) 
			{
				System.Environment.Exit(0);         //关闭窗体
			}
			else if (dr == DialogResult.Cancel)
			{
				e.Cancel = true;                  //不执行操作
			}
		}
		private void Form1_Load(object sender, EventArgs e)
		{
			// pictureBox画圆开始
			GraphicsPath gp = new GraphicsPath();
			gp.AddEllipse(pictureBox1.ClientRectangle);
			Region region = new Region(gp);
			pictureBox1.Region = region;
			gp.Dispose();
			region.Dispose();
			//画圆结束
			if (CH341DLL.CH341OpenDevice(Form1.mIndex) == new IntPtr(-1))
			{
				this.Text = " 写码板设备已拔出";
				Form1.m_open = false;
				this.pictureBox1.BackColor = Color.Red;
			}
			else
			{
				this.Text = " 写码板设备已插入";
				Form1.m_open = true;
				this.pictureBox1.BackColor = Color.Green;
			}
			this.comboBox1.SelectedText = "50ms";
			this.NOTIFY_ROUTINE = new CH341DLL.mPCH341_NOTIFY_ROUTINE(this.Device_Event);
			CH341DLL.CH341SetDeviceNotify(Form1.mIndex, null, this.NOTIFY_ROUTINE);
		}

		// 设备监控
		public void Device_Event(uint iEventStatus)
		{
			if (iEventStatus != 3U)
			{
				if (iEventStatus == 0U)
				{
					CH341DLL.CH341CloseDevice(Form1.mIndex);
					Form1.m_open = false;
					this.Text = "写码板设备已拔出";
					this.pictureBox1.BackColor = Color.Red;
				}
				return;
			}
			if (CH341DLL.CH341OpenDevice(Form1.mIndex) == new IntPtr(-1))
			{
				MessageBox.Show("打开设备失败!");
				Form1.m_open = false;
				this.Text = " 写码板设备已拔出";
				this.pictureBox1.BackColor = Color.Red;
				return;
			}
			Form1.m_open = true;
			this.Text = "写码板设备已插入";
			this.pictureBox1.BackColor = Color.Green;
		}
		public bool mClose()
		{
			CH341DLL.CH341CloseDevice(Form1.mIndex);
			Form1.m_open = false;
			return true;
		}

		// 读取模块按钮
		private void button2_Click(object sender, EventArgs e)
		{
			if (!Form1.m_open)
			{
				MessageBox.Show("请检查写码版是否插入", "连接写码版失败");
				return;
			}
			this.textBox2.Text = "";
			this.textBox7.Text = "";
			this.textBox2.Refresh();
			this.textBox7.Refresh();
			byte[] array = new byte[2];
			array[0] = 160;
			byte[] iWriteBuffer = array;
			byte[] array2 = new byte[128];
			if (!CH341DLL.CH341StreamI2C(0U, 2U, iWriteBuffer, 128U, array2))
			{
				MessageBox.Show("读码错误");
				return;
			}
			Thread.Sleep(30);
			string text = "";
			for (int i = 0; i < 128; i++)
			{
				text = text + array2[i].ToString("X02") + " ";
				if ((i + 1) % 16 == 0)
				{
					text += "\r\n";
				}
			}
			this.textBox2.Text = text;
			byte b = 0;
			byte b2 = 0;
			for (int i = 0; i < 63; i++)
			{
				b += array2[i];
			}
			for (int i = 64; i < 95; i++)
			{
				b2 += array2[i];
			}
			if (b == array2[63] && b2 == array2[95])
			{
				this.pictureBox2.BackColor = Color.Green;
			}
			else
			{
				this.pictureBox2.BackColor = Color.Red;
			}
			string text2 = "";
			for (int i = 20; i < 36; i++)
			{
				string str = text2;
				char c = (char)array2[i];
				text2 = str + c.ToString();
			}
			this.textBox3.Text = text2;
			string text3 = "";
			for (int i = 40; i < 56; i++)
			{
				string str2 = text3;
				char c = (char)array2[i];
				text3 = str2 + c.ToString();
			}
			this.textBox4.Text = text3;
			string text4 = "";
			for (int i = 84; i < 92; i++)
			{
				string str3 = text4;
				char c = (char)array2[i];
				text4 = str3 + c.ToString();
			}
			this.textBox5.Text = text4;
			string text5 = "";
			for (int i = 68; i < 84; i++)
			{
				string str4 = text5;
				char c = (char)array2[i];
				text5 = str4 + c.ToString();
			}
			this.textBox6.Text = text5;
			byte[] array3 = new byte[3];
			array3[0] = 162;
			array3[1] = 127;
			byte[] array4 = array3;
			if (!CH341DLL.CH341StreamI2C(0U, 3U, array4, 0U, array2))
			{
				MessageBox.Show("A2读失败");
				return;
			}
			Thread.Sleep(30);
			array4[1] = 128;
			if (!CH341DLL.CH341StreamI2C(0U, 2U, array4, 128U, array2))
			{
				MessageBox.Show("A2读失败");
				return;
			}
			text = "";
			for (int i = 0; i < 128; i++)
			{
				text = text + array2[i].ToString("X02") + " ";
				if ((i + 1) % 16 == 0)
				{
					text += "\r\n";
				}
			}
			this.textBox7.Text = text;
			array4[1] = 127;
			array4[2] = 1;
			if (!CH341DLL.CH341StreamI2C(0U, 3U, array4, 0U, array2))
			{
				MessageBox.Show("A2读失败");
				return;
			}
			Thread.Sleep(30);
			array4[1] = 128;
			if (!CH341DLL.CH341StreamI2C(0U, 2U, array4, 128U, array2))
			{
				MessageBox.Show("A2读失败");
				return;
			}
			text = "";
			for (int i = 0; i < 128; i++)
			{
				text = text + array2[i].ToString("X02") + " ";
				if ((i + 1) % 16 == 0)
				{
					text += "\r\n";
				}
			}
		}

		// 打开BIN文件按钮
		private void button1_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.RestoreDirectory = true;
			openFileDialog.Filter = "SFP码文件(*.bin,*.hex)|*.bin;*.hex";
			if (openFileDialog.ShowDialog() != DialogResult.OK)
			{
				MessageBox.Show("打开文件失败");
				return;
			}
			string fileName = openFileDialog.FileName;
			this.textBox1.Text = fileName;
			byte[] array = new byte[384];//声明字节组
			FileInfo fileInfo = new FileInfo(fileName);
			if (!fileInfo.Exists)
			{
				MessageBox.Show("文件不存在");
				return;
			}
			long length = fileInfo.Length;
			FileStream fileStream = new FileStream(fileName, FileMode.Open);
			fileStream.Read(array, 0, 384);
			fileStream.Close();
			for (int i = array.Length; i < 384; i++)
			{
				array[i] = 0;
			}
			if (length < 300L)
			{
				for (int j = 0; j < 128; j++)
				{
					array[j + 256] = array[j + 128];
				}
			}
			string str = "";
			for (int k = 0; k < 384; k++)
			{
				//测试A2信息
				char c = (char)array[k];
				Console.WriteLine(k + c.ToString());
				str = str + c.ToString();
			}
			//打印A2信息
			//Console.WriteLine(str);
			string text = "";
			for (int k = 0; k < 128; k++)
			{
				//Console.WriteLine(array[k].ToString("X02"));
				text = text + array[k].ToString("X02") + " ";
				if ((k + 1) % 16 == 0)
				{
					text += "\r\n";
				}
			}
			this.textBox10.Text = text;
			string text2 = "";
			for (int k = 20; k < 36; k++)
			{
				char c = (char)array[k];
				text2 = text2 + c.ToString();
			}
			this.textBox11.Text = text2;
			string text3 = "";
			for (int k = 40; k < 56; k++)
			{
				char c = (char)array[k];
				text3 = text3 + c.ToString();
			}
			this.textBox13.Text = text3;
			string text4 = "";
			for (int k = 84; k < 92; k++)
			{
				char c = (char)array[k];
				text4 = text4 + c.ToString();
			}
			this.textBox12.Text = text4;
			string text5 = "";
			for (int k = 68; k < 84; k++)
			{
				char c = (char)array[k];
				text5 = text5 + c.ToString();
			}
			this.textBox14.Text = text5;
			byte b = 0;
			byte b2 = 0;
			for (int k = 0; k < 63; k++)
			{
				b += array[k];
			}
			for (int k = 64; k < 95; k++)
			{
				b2 += array[k];
			}
			if (b == array[63] && b2 == array[95])
			{
				this.pictureBox3.BackColor = Color.Green;
			}
			else
			{
				this.pictureBox3.BackColor = Color.Red;
			}
			text = "";
			for (int k = 256; k < 384; k++)
			{
				text = text + array[k].ToString("X02") + " ";
				if ((k + 1) % 16 == 0)
				{
					text += "\r\n";
				}
			}
			this.textBox9.Text = text;
			string str1 = "";
			for (int k = 272; k < 279; k++)
			{
				//测试A2信息
				char c = (char)array[k];
				str1 = str1 + c.ToString();
			}
			//Console.WriteLine(str1);
		}

		// 修改模块信息
		private void button3_Click(object sender, EventArgs e)
		{
			byte[] array = new byte[128];
			array = Form1.getBYTE128(sender, this.textBox10);
			string text = this.textBox11.Text.Trim();
			while (text.Length < 16)
			{
				text += " ";
			}
			Console.WriteLine(Encoding.ASCII.GetBytes(text));
			byte[] bytes = Encoding.ASCII.GetBytes(text);
			for (int i = 20; i < 36; i++)
			{
				array[i] = bytes[i - 20];
			}
			string text2 = this.textBox13.Text.Trim();
			while (text2.Length < 16)
			{
				text2 += " ";
			}
			byte[] bytes2 = Encoding.ASCII.GetBytes(text2);
			for (int i = 40; i < 56; i++)
			{
				array[i] = bytes2[i - 40];
			}
			string text3 = this.textBox12.Text.Trim();
			if (text3.Length != 6)
			{
				MessageBox.Show("日期格式不对!请按此格式：180829");
				return;
			}
			while (text3.Length < 8)
			{
				text3 += " ";
			}
			byte[] bytes3 = Encoding.ASCII.GetBytes(text3);
			for (int i = 84; i < 92; i++)
			{
				array[i] = bytes3[i - 84];
			}
			string text4 = this.textBox14.Text.Trim();
			while (text4.Length < 16)
			{
				text4 += " ";
			}
			byte[] bytes4 = Encoding.ASCII.GetBytes(text4);
			for (int i = 68; i < 84; i++)
			{
				array[i] = bytes4[i - 68];
			}
			byte b = 0;
			byte b2 = 0;
			for (int i = 0; i < 63; i++)
			{
				b += array[i];
			}
			for (int i = 64; i < 95; i++)
			{
				b2 += array[i];
			}
			array[63] = b;
			array[95] = b2;
			string text5 = "";
			for (int i = 0; i < 128; i++)
			{
				text5 = text5 + array[i].ToString("X02") + " ";
				if ((i + 1) % 16 == 0)
				{
					text5 += "\r\n";
				}
			}
			this.textBox10.Text = text5;
			string text6 = "";
			for (int i = 20; i < 36; i++)
			{
				string str = text6;
				char c = (char)array[i];
				text6 = str + c.ToString();
			}
			this.textBox11.Text = text6;
			string text7 = "";
			for (int i = 40; i < 56; i++)
			{
				string str2 = text7;
				char c = (char)array[i];
				text7 = str2 + c.ToString();
			}
			this.textBox13.Text = text7;
			string text8 = "";
			for (int i = 84; i < 92; i++)
			{
				string str3 = text8;
				char c = (char)array[i];
				text8 = str3 + c.ToString();
			}
			this.textBox12.Text = text8;
			string text9 = "";
			for (int i = 68; i < 84; i++)
			{
				string str4 = text9;
				char c = (char)array[i];
				text9 = str4 + c.ToString();
			}
			this.textBox14.Text = text9;
			byte b3 = 0;
			byte b4 = 0;
			for (int i = 0; i < 63; i++)
			{
				b3 += array[i];
			}
			for (int i = 64; i < 95; i++)
			{
				b4 += array[i];
			}
			if (b3 == array[63] && b4 == array[95])
			{
				this.pictureBox3.BackColor = Color.Green;
				return;
			}
			this.pictureBox3.BackColor = Color.Red;
		}

		// 写码按钮
		private void button4_Click(object sender, EventArgs e)
		{
			if (!Form1.m_open)
			{
				MessageBox.Show("请检查写码版是否插入", "连接写码版失败");
				return;
			}
			if (this.textBox10.Text.Trim().Length <= 300 || this.textBox9.Text.Trim().Length <= 300)
			{
				MessageBox.Show("码错误");
				return;
			}
			Thread.Sleep(20);
			byte[] array = new byte[128];
			byte[] array2 = new byte[128];
			array = Form1.getBYTE128(sender, this.textBox10);
			array2 = Form1.getBYTE128(sender, this.textBox9);
			byte[] array3 = new byte[10];
			array3[0] = 160;
			array3[1] = 0;
			byte[] oReadBuffer = new byte[1];
			for (int i = 0; i < 128; i++)
			{
				array3[i % 8 + 2] = array[i];
				if ((i + 1) % 8 == 0)
				{
					array3[1] = (byte)(i - 7);
					Console.WriteLine(BitConverter.ToString(array3));
					if (!CH341DLL.CH341StreamI2C(0U, 10U, array3, 1U, oReadBuffer))
					{
						MessageBox.Show("写入错误");
						return;
					}
					Thread.Sleep(this.time_a);
				}
			}
			byte[] array4 = new byte[3];
			array4[0] = 162;
			array4[1] = 127;
			byte[] iWriteBuffer2 = array4;
			if (!CH341DLL.CH341StreamI2C(0U, 3U, iWriteBuffer2, 0U, oReadBuffer))
			{
				MessageBox.Show("写入错误");
				return;
			}
			Thread.Sleep(30);
			array3[0] = 162;
			for (int i = 128; i < 248; i++)
			{
				array3[i % 8 + 2] = array2[i - 128];
				if ((i + 1) % 8 == 0)
				{
					array3[1] = (byte)(i - 7);
					if (!CH341DLL.CH341StreamI2C(0U, 10U, array3, 0U, oReadBuffer))
					{
						MessageBox.Show("写入错误");
						return;
					}
					Thread.Sleep(this.time_a);
				}
			}

			this.button2_Click(sender, e);
			string text = this.textBox10.Text;
			string a = this.textBox9.Text.Substring(0, 373);
			string text2 = this.textBox2.Text;
			string b = this.textBox7.Text.Substring(0, 373);
			if (text != text2)
			{
				this.pictureBox4.BackColor = Color.Red;
				return;
			}
			if (this.checkBox3.Checked)
			{
				if (a != b)
				{
					this.pictureBox4.BackColor = Color.Red;
					return;
				}
				this.pictureBox4.BackColor = Color.Green;
				this.autowrit(sender, e);
				return;
			}
			else
			{
				this.pictureBox4.BackColor = Color.Green;
				this.autowrit(sender, e);
				return;
			}
		}
		private void autowrit(object sender, EventArgs e)
		{
			if (this.checkBox4.Checked)
			{
				this.x++;
				if (this.x >= this.name.Length)
				{
					MessageBox.Show("已写完最后一个");
					return;
				}
				string text = this.name[this.x];
				this.textBox1.Text = text;
				this.label12.Text = (this.x + 1).ToString();
				byte[] array = new byte[384];
				string text2 = text;
				FileInfo fileInfo = new FileInfo(text2);
				if (!fileInfo.Exists)
				{
					MessageBox.Show("文件不存在");
					return;
				}
				long length = fileInfo.Length;
				FileStream fileStream = new FileStream(text2, FileMode.Open);
				fileStream.Read(array, 0, 384);
				fileStream.Close();
				for (int i = array.Length; i < 384; i++)
				{
					array[i] = 0;
				}
				if (length < 300L)
				{
					for (int j = 0; j < 128; j++)
					{
						array[j + 256] = array[j + 128];
					}
				}
				text = "";
				for (int k = 0; k < 128; k++)
				{
					text = text + array[k].ToString("X02") + " ";
					if ((k + 1) % 16 == 0)
					{
						text += "\r\n";
					}
				}
				this.textBox10.Text = text;
				string text3 = "";
				for (int k = 20; k < 36; k++)
				{
					string str = text3;
					char c = (char)array[k];
					text3 = str + c.ToString();
				}
				this.textBox11.Text = text3;
				string text4 = "";
				for (int k = 40; k < 56; k++)
				{
					string str2 = text4;
					char c = (char)array[k];
					text4 = str2 + c.ToString();
				}
				this.textBox13.Text = text4;
				string text5 = "";
				for (int k = 84; k < 92; k++)
				{
					string str3 = text5;
					char c = (char)array[k];
					text5 = str3 + c.ToString();
				}
				this.textBox12.Text = text5;
				string text6 = "";
				for (int k = 68; k < 84; k++)
				{
					string str4 = text6;
					char c = (char)array[k];
					text6 = str4 + c.ToString();
				}
				this.textBox14.Text = text6;
				byte b = 0;
				byte b2 = 0;
				for (int k = 0; k < 63; k++)
				{
					b += array[k];
				}
				for (int k = 64; k < 95; k++)
				{
					b2 += array[k];
				}
				if (b == array[63] && b2 == array[95])
				{
					this.pictureBox3.BackColor = Color.Green;
				}
				else
				{
					this.pictureBox3.BackColor = Color.Red;
				}
				text = "";
				for (int k = 256; k < 384; k++)
				{
					text = text + array[k].ToString("X02") + " ";
					if ((k + 1) % 16 == 0)
					{
						text += "\r\n";
					}
				}
				this.textBox9.Text = text;
			}
			if (this.checkBox9.Checked)
			{
				string text7 = this.textBox14.Text;
				text7 = text7.Trim();
				string text8 = text7.Substring(text7.Length - 4, 4);
				int num = Convert.ToInt32(text8);
				if (num == 9999)
				{
					MessageBox.Show("已到上限请手动修改SN");
					return;
				}
				text8 = (num + 1).ToString("D4");
				text7 = text7.Substring(0, text7.Length - 4);
				text7 += text8;
				this.textBox14.Text = text7;
				this.button3_Click(sender, e);
			}
		}
		private void pictureBox3_BackColorChanged(object sender, EventArgs e)
		{
			if (this.pictureBox3.BackColor == Color.Green)
			{
				this.Button4.Enabled = true;
				return;
			}
			this.Button4.Enabled  = false;
		}
		public static byte[] getBYTE128(object sender, TextBox box)
		{
			byte[] array = new byte[128];
			string text = box.Text;
			text.Trim();
			if (text.Length == 0)
			{
				MessageBox.Show("数据为空");
				return array;
			}
			text = text.Replace("\r\n", "");
			string[] array2 = new string[128];
			array2 = text.Split(new char[]
			{
				' '
			});
			for (int i = 0; i < 128; i++)
			{
				array[i] = Convert.ToByte(array2[i], 16);
				
			}

			//Console.WriteLine(BitConverter.ToString(array));
			return array;
		}

		// 保存BIN文件
		private void button5_Click(object sender, EventArgs e)
		{
			byte[] array = new byte[128];
			array = Form1.getBYTE128(sender, this.textBox10);
			byte[] array2 = new byte[128];
			byte[] array3 = new byte[384];
			array2 = Form1.getBYTE128(sender, this.textBox9);
			for (int i = 0; i < 128; i++)
			{
				array3[i + 256] = array2[i];
			}
			for (int i = 0; i < 128; i++)
			{
				array3[i] = array[i];
			}
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.RestoreDirectory = true;
			saveFileDialog.Filter = "SFP码文件(*.bin)|*.bin";
			saveFileDialog.FileName = this.textBox11.Text.Trim()+this.textBox14.Text.Trim();

			if (saveFileDialog.ShowDialog() != DialogResult.OK)
			{
				//MessageBox.Show("失败");
				return;
			}
			FileStream fileStream = new FileStream(saveFileDialog.FileName, FileMode.Create);
			fileStream.Write(array3, 0, 384);
			fileStream.Close();
			MessageBox.Show("保存文件成功");
		}

		// A2高位填充00
		private void button8_Click(object sender, EventArgs e)
		{
			byte[] array = new byte[128];
			string text = "";
			for (int i = 0; i < 128; i++)
			{
				text = text + array[i].ToString("X02") + " ";
				if ((i + 1) % 16 == 0)
				{
					text += "\r\n";
				}
			}
			this.textBox9.Text = text;
		}

		// 读取数据填充到编辑区按钮
		private void button6_Click(object sender, EventArgs e)
		{
			if (this.textBox2.Text.Trim().Length == 0 || this.textBox7.Text.Trim().Length == 0)
			{
				MessageBox.Show("数据不能为空");
				return;
			}
			this.textBox10.Text = this.textBox2.Text;
			this.textBox9.Text = this.textBox7.Text;
			byte[] array = new byte[128];
			array = Form1.getBYTE128(sender, this.textBox10);
			string text = "";
			for (int i = 20; i < 36; i++)
			{
				string str = text;
				char c = (char)array[i];
				text = str + c.ToString();
			}
			this.textBox11.Text = text;
			string text2 = "";
			for (int i = 40; i < 56; i++)
			{
				string str2 = text2;
				char c = (char)array[i];
				text2 = str2 + c.ToString();
			}
			this.textBox13.Text = text2;
			string text3 = "";
			for (int i = 84; i < 92; i++)
			{
				string str3 = text3;
				char c = (char)array[i];
				text3 = str3 + c.ToString();
			}
			this.textBox12.Text = text3;
			string text4 = "";
			for (int i = 68; i < 84; i++)
			{
				string str4 = text4;
				char c = (char)array[i];
				text4 = str4 + c.ToString();
			}
			this.textBox14.Text = text4;
			byte b = 0;
			byte b2 = 0;
			for (int i = 0; i < 63; i++)
			{
				b += array[i];
			}
			for (int i = 64; i < 95; i++)
			{
				b2 += array[i];
			}
			if (b == array[63] && b2 == array[95])
			{
				this.pictureBox3.BackColor = Color.Green;
				return;
			}
			this.pictureBox3.BackColor = Color.Red;
		}
		private void textBox12_TextChanged(object sender, EventArgs e)
		{
			if (this.textBox12.Text.Trim().Length >= 6)
			{
				this.textBox12.Text = this.textBox12.Text.Trim();
				this.textBox12.ForeColor = Color.Black;
				return;
			}
			this.textBox12.ForeColor = Color.Red;
		}
		//写密码按钮
		private void button15_Click(object sender, EventArgs e)
		{
			if (!Form1.m_open)
			{
				MessageBox.Show("请先连接USB");
				return;
			}
			string text = this.textBox15.Text;
			if (text.Length != 8)
			{
				MessageBox.Show("密码格式错误");
				return;
			}
			string[] array = new string[4];
			byte[] array2 = new byte[]
			{
				162,
				123,
				byte.MaxValue,
				byte.MaxValue,
				byte.MaxValue,
				byte.MaxValue
			};
			for (int i = 0; i < 4; i++)
			{
				array[i] = text.Substring(i * 2, 2);
				array2[i + 2] = Convert.ToByte(array[i], 16);
			}
			byte[] oReadBuffer = new byte[1];
            Console.WriteLine(BitConverter.ToString(array2));
            
            if (!CH341DLL.CH341StreamI2C(0U, 6U, array2, 0U, oReadBuffer))
			{
				MessageBox.Show("错误");
				return;
			}
		}
		protected override bool ProcessDialogKey(Keys keyData)
		{
			return keyData != Keys.Return && base.ProcessDialogKey(keyData);
		}

		// 文件夹序列化打开
		private void checkBox4_CheckedChanged(object sender, EventArgs e)
		{
			if (this.checkBox4.Checked)
			{
				OpenFileDialog openFileDialog = new OpenFileDialog();
				openFileDialog.Filter = "SFP码文件(*.bin)|*.bin";
				if (openFileDialog.ShowDialog() != DialogResult.OK)
				{
					return;
				}
				string text = openFileDialog.FileName;
				text = text.Substring(0, text.LastIndexOf('\\'));
				this.textBox1.Text = openFileDialog.FileName;
				this.name = Directory.GetFiles(text, "*.bin");
				Array.Sort<string>(this.name);
				this.x = Array.IndexOf<string>(this.name, openFileDialog.FileName);
				this.label12.Text = (this.x + 1).ToString();
				this.label13.Text = "/" + this.name.Length.ToString();
				byte[] array = new byte[384];
				string fileName = openFileDialog.FileName;
				FileInfo fileInfo = new FileInfo(fileName);
				if (!fileInfo.Exists)
				{
					MessageBox.Show("文件不存在");
					return;
				}
				long length = fileInfo.Length;
				FileStream fileStream = new FileStream(fileName, FileMode.Open);
				fileStream.Read(array, 0, 384);
				fileStream.Close();
				for (int i = array.Length; i < 384; i++)
				{
					array[i] = 0;
				}
				if (length < 300L)
				{
					for (int j = 0; j < 128; j++)
					{
						array[j + 256] = array[j + 128];
					}
				}
				string text2 = "";
				for (int k = 0; k < 128; k++)
				{
					text2 = text2 + array[k].ToString("X02") + " ";
					if ((k + 1) % 16 == 0)
					{
						text2 += "\r\n";
					}
				}
				this.textBox10.Text = text2;
				string text3 = "";
				for (int k = 20; k < 36; k++)
				{
					string str = text3;
					char c = (char)array[k];
					text3 = str + c.ToString();
				}
				this.textBox11.Text = text3;
				string text4 = "";
				for (int k = 40; k < 56; k++)
				{
					string str2 = text4;
					char c = (char)array[k];
					text4 = str2 + c.ToString();
				}
				this.textBox13.Text = text4;
				string text5 = "";
				for (int k = 84; k < 92; k++)
				{
					string str3 = text5;
					char c = (char)array[k];
					text5 = str3 + c.ToString();
				}
				this.textBox12.Text = text5;
				string text6 = "";
				for (int k = 68; k < 84; k++)
				{
					string str4 = text6;
					char c = (char)array[k];
					text6 = str4 + c.ToString();
				}
				this.textBox14.Text = text6;
				byte b = 0;
				byte b2 = 0;
				for (int k = 0; k < 63; k++)
				{
					b += array[k];
				}
				for (int k = 64; k < 95; k++)
				{
					b2 += array[k];
				}
				if (b == array[63] && b2 == array[95])
				{
					this.pictureBox3.BackColor = Color.Green;
				}
				else
				{
					this.pictureBox3.BackColor = Color.Red;
				}
				text2 = "";
				for (int k = 256; k < 384; k++)
				{
					text2 = text2 + array[k].ToString("X02") + " ";
					if ((k + 1) % 16 == 0)
					{
						text2 += "\r\n";
					}
				}
				this.textBox9.Text = text2;
				this.checkBox9.Checked = false;
				this.label12.Visible = true;
				this.label13.Visible = true;
				return;
            }
            else
            {
				this.label12.Visible = false;
				this.label13.Visible = false;
			}
		}

		// SN序列化选择框
		private void checkBox9_CheckedChanged(object sender, EventArgs e)
		{
			if (this.checkBox9.Checked)
			{
				this.checkBox4.Checked = false;
			}
		}
		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			string text = this.comboBox1.Text;
			text = text.Replace("ms", "");
			this.time_a = Convert.ToInt32(text);
		}
		private void button4_MouseDown(object sender, MouseEventArgs e)
		{
			this.pictureBox4.BackColor = Color.Yellow;
			this.pictureBox2.BackColor = Color.Yellow;
		}
		private void button4_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if (e.KeyCode == Keys.Space)
			{
				this.pictureBox4.BackColor = Color.Yellow;
				this.pictureBox2.BackColor = Color.Yellow;
			}
		}
		private void textBox14_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			if (e.KeyCode == Keys.Return)
			{
				this.button3_Click(sender, e);
				this.pictureBox4.BackColor = Color.Yellow;
				this.pictureBox2.BackColor = Color.Yellow;
				this.pictureBox4.Refresh();
				this.pictureBox2.Refresh();
				this.button4_Click(sender, e);
				this.textBox14.Text = "";
			}
		}
		public static uint mIndex;
		public static bool m_open;
		public static IntPtr mainhwnd;
		private int time_a = 50;
		private CH341DLL.mPCH341_NOTIFY_ROUTINE NOTIFY_ROUTINE;
		private bool pw_flg;
		private string[] name = new string[4096];
		private int x;
		// 编辑A0信息按钮
		private void Button9_Click(object sender, EventArgs e)
        {
           /* if (!Form1.m_open)
            {
                MessageBox.Show("请检查写码版是否插入", "连接写码版失败");
                return;
            }*/
            Form form = new Form2();
			Form2.str_aa = this.textBox10.Text;
			if (form.ShowDialog() != DialogResult.OK)
			{
				return;
			}
			this.textBox10.Text = Form2.str_aa;
			this.button3_Click(sender, e);
		}
    }
}
